package com.itheima.health.utils.myutil;

import com.itheima.health.utils.date.DateUtils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("ALL")
/**
 * @Description :自定义对象转换
 * @author     :LD
 */
public class MyBeanUtil {
    public static <T> List<T> ObjectToBeanList(List<String[]> list,Class<T> clazz)throws Exception{
        if (list==null||list.size()==0) return null;

        //目标构造方法的参数Class[]
        Class<?>[] cz=null;
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            Class<?>[] types = constructor.getParameterTypes();
            if (list.get(0).length==types.length){
                cz=types;
                break;
            }
        }

        List<T> result = new ArrayList<>();
        for (String[] obj : list) {
            Constructor<T> constructor = clazz.getConstructor(cz);
            result.add(constructor.newInstance(DateUtils.parseString2Date(obj[0],"yyyy/MM/dd"),Integer.valueOf(obj[1])));
        }
        return result;
    }

}
